package com.qf.game.websocket.entity;

import com.qf.game.utils.RoomManagerUtils;
import lombok.Data;
import lombok.experimental.Accessors;

import javax.websocket.Session;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 房间对象
 *
 * description:
 * author: Ken
 * 公众号：Java架构栈
 */
@Data
@Accessors(chain = true)
public class Room implements Serializable {

    private Integer rid;
    private String title;
    private Integer type;//0-公开 1-私密
    private String password;//密码
    private Integer islook;//0-可观战 1-不可观战
    private Integer status = 0;//房间状态 0-待玩家加入 1-对局已经开始

    private PlayerInfo playerInfo1;//玩家1
    private PlayerInfo playerInfo2;//玩家2
    private List<PlayerInfo> looks;//观战的玩家信息

    private ChessBoard chessBoard;//棋局对象

//    public Room(){
//        System.out.println("房间的无参构造已经调用！！！！！！");
//        //房间的id生成器
//        rid = IdGeneratorUtils.generatorId();
//        //构建棋盘对象
//        chessBoard = new ChessBoard(15);
//    }

    /**
     * 获得房间的玩家人数
     * @return
     */
    public int getPlayerNum(){
        int count = 0;
        if (playerInfo1 != null) {
            count++;
        }
        if (playerInfo2 != null) {
            count++;
        }
        return count;
    }

    //添加玩家对象
    public int addPlayerInfo(PlayerInfo playerInfo){
        //将该玩家的session和当前房间进行关联
        RoomManagerUtils.addRoomSession(playerInfo.getSession(), this);
        if (playerInfo1 == null) {
            playerInfo1 = playerInfo;
            return 0;
        } else {
            playerInfo2 = playerInfo;
            return 1;
        }
    }

    //添加观战的玩家信息
    public void addLookPlayerInfo(PlayerInfo playerInfo){
        if (looks == null) {
            looks = new ArrayList<>();
        }
        RoomManagerUtils.addRoomSession(playerInfo.getSession(), this);
        looks.add(playerInfo);
    }

    //根据当前玩家的session获取对手玩家的session
    public PlayerInfo getOtherPlayer(Session session) {
        if (playerInfo1.getSession() == session) {
            return playerInfo2;
        } else {
            return playerInfo1;
        }
    }

    //根据session获取当前玩家本身的信息
    public PlayerInfo getPlayer(Session session){
        if (playerInfo1.getSession() == session) {
            return playerInfo1;
        } else {
            return playerInfo2;
        }
    }

    public void removePlayer(Session session){
        if (playerInfo1.getSession() == session) {
            playerInfo1 = null;
        } else {
            playerInfo2 = null;
        }
    }
}
